﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;

namespace TRPOP.Common
{
    /// <summary>
    /// UploadHelper
    /// </summary>
    public class UploadHelper
    {
        #region 上传多个文件 UploadFiles
        /// <summary>
        ///  上传多文件
        /// </summary>
        /// <param name="upLoadForder">上传目录，相对于根目录，前不用加"/"</param>     
        /// <param name="maxSize">文件最大限制</param>
        /// <param name="allowImageTypes">允许的文件类型</param>
        /// <returns></returns>
        static public AttachmentInfo[] UploadFiles(string upLoadForder, int maxSize, List<string> allowImageTypes)
        {

            HttpFileCollection uloadCollection = HttpContext.Current.Request.Files;

            //算出文件数量
            int intFileCount = uloadCollection.Count;
            //
            AttachmentInfo[] attachInfos = new AttachmentInfo[intFileCount];

            //循环上传所有的文件
            for (int i = 0; i < intFileCount; i++)
            {
                attachInfos[i] = new AttachmentInfo();

                if (!uloadCollection[i].FileName.Equals(""))
                {
                    //文件名
                    string strFileName = uloadCollection[i].FileName;
                    //文件扩展名
                    string strFileExtendName = strFileName.Substring(strFileName.LastIndexOf(".") + 1).ToLower();
                    //文件真实类型
                    string strFileType = uloadCollection[i].ContentType;
                    //类型验证 待做

                    //文件大小
                    int intFileSize = uloadCollection[i].ContentLength;
                    if (maxSize > 0)
                    {
                        if (intFileSize > maxSize)
                        {
                            attachInfos[i].ErrorDescription = "对不起，文件上最大不能超过" + maxSize / 1000 + "K，请重新选择上传！";//2010-05-31 梁伟 将提示信息中的maxSize 改成maxSize/1000
                        }
                        else
                        {
                            attachInfos[i] = UploadFile(uloadCollection[i], upLoadForder);
                        }
                    }
                    else
                    {
                        attachInfos[i] = UploadFile(uloadCollection[i], upLoadForder);
                    }

                }
            }
            return attachInfos;
        }
        #endregion

        #region 上传单个文件 UploadImage
        /// <summary>
        /// 上传单个文件
        /// </summary>
        /// <param name="postedFile">上传控件</param>
        /// <param name="upLoadForder">上传目录，相对于根目录，前不用加"/"</param>
        /// <returns></returns>
        static public AttachmentInfo UploadFile(HttpPostedFile postedFile, string upLoadForder)
        {
            AttachmentInfo oaiInfo = new AttachmentInfo();
            string strRootFolder = "/";
            //文件名
            string strFileName = postedFile.FileName;
            //文件扩展名
            string strFileExtendName = strFileName.Substring(strFileName.LastIndexOf(".") + 1).ToLower();
            //文件真实类型
            string strFileType = postedFile.ContentType;
            //文件大小
            int intFileSize = postedFile.ContentLength;
            //建立新的时间文件文件夹
            string strNowFolder = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
            //文件上传的文件夹目录（绝对路径）
            string strFolderMapPath;
            if (!upLoadForder.Equals(""))
            {
                strFolderMapPath = Util.GetMapPath(strRootFolder + upLoadForder + "/" + strNowFolder.ToString());
            }
            else
            {
                strFolderMapPath = Util.GetMapPath(strRootFolder + strNowFolder.ToString());
            }
            //取当前时间的字符串                    
            string strNowTimeString = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
            //文件的新名称
            string strNewFileName = strNowTimeString + intFileSize.ToString() + "." + strFileExtendName;
            //最终保存文件的路径
            string strFileMapPath = strFolderMapPath + "/" + strNewFileName;
            //返回的文件虚拟目录(相对路径)
            string strFileUploadPath = upLoadForder + "/" + strNowFolder.ToString() + "/" + strNewFileName;
            try
            {
                //加上类型判断(判断是否为系统允许的上传类型,判断文件大小) 
                Directory.CreateDirectory(strFolderMapPath);

                //保存文件
                postedFile.SaveAs(strFileMapPath);

            }
            catch
            {
                oaiInfo.ErrorDescription = "上传文件过程出现故障";
            }
            //HttpContext.Current.Response.Write(strFileUploadPath);
            //附件属性
            oaiInfo.FileName = strRootFolder + strFileUploadPath;
            oaiInfo.FileSize = intFileSize;
            oaiInfo.Attachment = strFileName.Substring(strFileName.LastIndexOf('\\') + 1);
            oaiInfo.FileType = strFileExtendName;
            return oaiInfo;
        }
        #endregion
    }
}
